Skip to content

test: adopt @cloudflare/vitest-pool-workers, delete SQL-parser mock#9

Merged
fullstackjam merged 4 commits into
mainfrom
worktree-vitest-pool-workers-spike
May 17, 2026
Merged

test: adopt @cloudflare/vitest-pool-workers, delete SQL-parser mock#9
fullstackjam merged 4 commits into
mainfrom
worktree-vitest-pool-workers-spike

Conversation

@fullstackjam
Copy link
Copy Markdown
Contributor

Summary

  • Migrates 14 server tests (181 tests total) from a SQL-parsing mock D1 to real D1 inside the Workers runtime via @cloudflare/vitest-pool-workers. Pure-lib tests + archtests stay on happy-dom (6 files, 99 tests).
  • Deletes src/lib/test/db-mock.ts (~390 lines of brittle SQL parser). Net diff: +2,480 / −5,412.
  • Bumps vitest 4.0.184.1.x (pool-workers peer dep).
  • Fixes migration 0002 from SQLite-illegal ALTER TABLE ADD COLUMN ... UNIQUE to ADD COLUMN + CREATE UNIQUE INDEX. Safe for prod (already applied), unblocks local D1.
  • Real-D1 surfaced 3 hidden test bugs (duplicate UNIQUE alias values the mock silently allowed) — fixed inline.
  • Two smoke tests upgraded from fake-state to true end-to-end:
    • cli-auth-flow now calls the real approve endpoint (mock couldn't handle datetime expressions before).
    • config-crud now verifies UPDATE side effects in the DB (mock didn't honor COALESCE).
  • Removes test-only process.env shim in setup.ts (aligns with the existing no-process-env archtest rule).

Test plan

  • npm run test:happy-dom → 99 / 99 (6 files)
  • npm run test:workers → 181 / 181 (14 files)
  • npm run validate → lint clean, 0 svelte-check errors, both suites green, build OK
  • After merge: confirm CI's existing check job still passes against the bumped vitest

Notes for reviewer

  • vitest.workers.config.ts runs the pool-workers suite separately; both configs are wired into npm test, plus targeted scripts test:happy-dom and test:workers.
  • New helpers: src/lib/test/{call,seed,apply-migrations}.tscall(handler, opts) replaces ~30 lines of RequestEvent boilerplate per test; seed(db, rows) is a typed inserter; applyD1Migrations runs in-memory before each suite.
  • Schema-only migrations are applied to the test D1; data migrations (seed/transfer/enrich) reference prod-specific user IDs and are filtered out by vitest.workers.config.ts. Tests seed their own fixtures.

fullstackjam and others added 4 commits May 17, 2026 12:59
Migrates 4 server tests from the SQL-parsing mock DB to real D1 running
inside the Workers runtime via @cloudflare/vitest-pool-workers. Existing
tests on happy-dom remain untouched.

Changes:
- Adds vitest.workers.config.ts (cloudflareTest plugin + cloudflarePool)
  scoped to the 4 migrated tests; filters out data-only migrations
- Adds src/lib/test/{apply-migrations,seed,call}.ts as the new test
  infrastructure (real-D1 seeding, FK-aware reset, minimal RequestEvent
  builder)
- Migrates health, db/configs, api/configs, and api/auth/cli/poll tests
- Bumps vitest 4.0.18 -> 4.1.x (pool-workers peer dep)
- Fixes migration 0002 to use ADD COLUMN + CREATE UNIQUE INDEX instead
  of ALTER TABLE ADD COLUMN ... UNIQUE (SQLite-illegal; hosted D1 had
  tolerated it). Safe for prod, already applied
- Fixes 3 hidden test bugs where fixtures duplicated UNIQUE alias
  values; the SQL-parser mock didn't enforce the constraint

After: 48 tests on pool-workers (4 files), 232 on happy-dom (16 files),
all green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Migrates the remaining 9 server tests to vitest-pool-workers and removes
the fragile SQL-parsing mock DB. Pure-library tests (rate-limit,
install-script, validation) and archtests stay on happy-dom — they don't
need D1 or the Workers runtime.

Migrated to pool-workers in this commit:
- src/lib/server/auth.test.ts (JWT + DB-backed getCurrentUser)
- src/routes/api/auth/cli/start/server.test.ts
- src/routes/api/auth/cli/approve/server.test.ts
- src/routes/api/configs/[slug]/server.test.ts
- src/routes/api/configs/[slug]/revisions/server.test.ts
- src/routes/[username]/[slug]/install/server.test.ts
- src/routes/[username]/[slug]/config/server.test.ts
- src/smoke-tests/{cli-auth-flow,config-crud,critical-paths}.test.ts

The CLI auth-flow smoke test now exercises the real approve endpoint
end-to-end (previously had to fake it because the mock couldn't handle
datetime expressions). The config-crud smoke test now verifies UPDATE
side effects in the DB (previously couldn't — mock didn't honor
COALESCE).

Infrastructure:
- Adds src/lib/test/call.ts helper to cut RequestEvent boilerplate
- Extends call() with params/clientAddress for routes that need them
- Deletes src/lib/test/db-mock.ts (~390 lines of SQL-parsing mock)
- Deletes src/lib/test/setup.ts (process.env shims no longer needed)
- Deletes src/lib/test/helpers.ts (unused after migration)
- Trims src/lib/test/fixtures.ts to data-only exports
- Adds @cloudflare/vitest-pool-workers/types to tsconfig.json
- Wires npm test / test:run / test:coverage to run both configs;
  adds test:happy-dom and test:workers for targeted runs

Final test layout: 181 tests on pool-workers (14 files, real D1 +
Workers runtime), 99 tests on happy-dom (6 files, pure libs +
archtests). Both green; full npm run validate clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
CI Node 20 → 22 because @cloudflare/vitest-pool-workers transitively
pulls @cloudflare/kv-asset-handler@0.5.0 which requires Node ≥22.

knip drift sensor was flagging every `cloudflare:test` import as an
unlisted dependency (it doesn't grok the virtual-module convention).
Added `cloudflare` to knip.json's ignoreDependencies so the sensor stops
opening tracking issues for this. The package itself is provided by
@cloudflare/vitest-pool-workers and not a real dependency entry.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
workerd has no node:inspector/promises module, which @vitest/coverage-v8
requires. Coverage now runs only on the happy-dom suite; the
pool-workers suite still runs (verifying behavior) but does not
contribute to the coverage report. Trade-off: covered % drops because
many tests moved to pool-workers. Acceptable — tests still execute,
and switching the coverage backend (e.g. istanbul) is a separate
decision.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@fullstackjam fullstackjam merged commit 77166b5 into main May 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant